//
//  LSHomeViewController.m
//  KuaiYi
//
//  Created by ZengFanSheng on 2016/11/13.
//  Copyright © 2016年 HeiMa. All rights reserved.
//

#import "LSHomeViewController.h"
#import "LSWeatherView.h"
#import "SDCycleScrollView.h"
#import "LSTextSlideView.h"
#import <Masonry.h>
#import "NetworkTool.h"
#import "LSWeatherModel.h"
#import "LSFlowLayout.h"
#import "LSRegistrationCell.h"
#import "LSRegistrationModel.h"
#import "LSRegistrationController.h"

static NSString *registrationCell = @"registrationCell";
@interface LSHomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,SDCycleScrollViewDelegate>

/* 天气 */
@property (weak, nonatomic) LSWeatherView *weatherView;
/* 图片轮播 */
@property (weak, nonatomic) SDCycleScrollView *cycleScrollView;
/* 文字轮播 */
@property (weak, nonatomic) LSTextSlideView *textSlideView;
/* 挂号 */
@property (weak, nonatomic) UICollectionView *registrationView;

/* 挂号模型数组 */
@property (strong, nonatomic) NSArray *registrations;

@end

@implementation LSHomeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self getWeather];
    [self getSlide];
    
    // 获取挂号入口的模型数组
    self.registrations = [LSRegistrationModel loadRegistrationData];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LSRegistrationCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:registrationCell forIndexPath:indexPath];
    LSRegistrationModel *registration = self.registrations[indexPath.item];
    cell.registration = registration;
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LSRegistrationController *vc = [[LSRegistrationController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)setupUI
{
    // 创建天气View
    UINib *nib =[UINib nibWithNibName:@"LSWeatherView" bundle:nil];
    LSWeatherView *weatherView = [nib instantiateWithOwner:self options:nil].lastObject;
    [self.view addSubview: weatherView];
    
    // 创建图片轮播
    SDCycleScrollView *slideView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:nil];
    [self.view addSubview:slideView];
    
    // 创建文字轮播视图
    LSTextSlideView *textSlideView = [[LSTextSlideView alloc] init];
    [self.view addSubview:textSlideView];
    
    // 创建挂号视图
    LSFlowLayout *flowLayout = [[LSFlowLayout alloc] init];
    UICollectionView *registrationView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    registrationView.dataSource = self;
    registrationView.delegate = self;
    [registrationView registerClass:[LSRegistrationCell class] forCellWithReuseIdentifier:registrationCell];
    registrationView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:registrationView];
    
    // 自动布局
    [weatherView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.mas_topLayoutGuideBottom);
        make.height.mas_equalTo(70.5);
    }];
    [slideView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weatherView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(textSlideView.mas_top);
    }];
    [textSlideView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(registrationView.mas_top);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(44.5);
    }];
    [registrationView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(260);
        make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
    }];
    
    // 记录控件
    self.weatherView = weatherView;
    self.cycleScrollView = slideView;
    self.textSlideView = textSlideView;
    self.registrationView = registrationView;
}

#pragma mark - 加载数据

/**
 加载天气的数据
 */
- (void)getWeather{
    NSString *key = @"17IvzuqmKrK1cGwNL6VQebF9";
    
    NSString *URLStr = @"http://api.map.baidu.com/telematics/v3/weather";
    
    NSDictionary *parameters = @{@"location":@"深圳",@"output":@"json",@"ak":key};
    
    [[LSWeatherModel shareModel]loadWeatherDataWithURLStr:URLStr parameters:parameters success:^(NSArray *newsList) {
        
        self.weatherView.weathers = newsList;
        
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}


/**
 加载首页轮播器数据
 */
- (void)getSlide
{
    //首页轮播图接口
    NSString *url = @"http://iosapi.itcast.cn/doctor/banners.json.php";
    NSDictionary *parameters = @{@"page_size":@10,@"page":@1};
    [[NetworkTool sharedTool] POSTWithURLStr:url parameters:parameters success:^(NSDictionary *responseObject) {
        NSDictionary *dataDict = responseObject[@"data"];
        NSArray *dictArr = dataDict[@"banners"];
        NSMutableArray *imgURLs = [NSMutableArray arrayWithCapacity:dictArr.count];
        for (NSDictionary *banners in dictArr) {
            
            [imgURLs addObject:banners[@"banner_img_url"]];
        }
        // 回到主线程刷新UI
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.cycleScrollView.imageURLStringsGroup = imgURLs;
        }];
        
    } failed:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];
}



@end
